package com.bruce.starts.demo.commonerror.datetime;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Currency;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author luotuan
 * @Description
 * @create 2021-05-12 14:49
 **/
@Slf4j
public class DateDemo {
    public static void main(String[] args) {
        //wrong();
        //right();
        System.out.println(LocalDateTime.now().with(temporal -> temporal.plus(ThreadLocalRandom.current().nextInt(100), ChronoUnit.DAYS)));
    }
    public static void wrong() {
        log.info("wrong");
        Date date = new Date(2021, 5, 12, 14, 50, 33);
        System.out.println(date);
    }

    private static void right(){
        String stringDate = "2021-05-12 22:00:00";
        ZoneId timeZoneSH = ZoneId.of("Asia/Shanghai");
        ZoneId timeZoneNY = ZoneId.of("America/New_York");
        ZoneId timeZoneJST = ZoneOffset.ofHours(9);
        DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
                .appendValue(ChronoField.YEAR)
                .appendLiteral("-")
                .appendValue(ChronoField.MONTH_OF_YEAR)
                .appendLiteral("-")
                .appendValue(ChronoField.DAY_OF_MONTH)
                .appendLiteral(" ")
                .appendValue(ChronoField.HOUR_OF_DAY)
                .appendLiteral(":")
                .appendValue(ChronoField.MINUTE_OF_HOUR)
                .appendLiteral(":")
                .appendValue(ChronoField.SECOND_OF_MINUTE)
                .toFormatter();
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ZonedDateTime dateTime = ZonedDateTime.of(LocalDateTime.parse(stringDate,dateTimeFormatter),timeZoneJST);
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
        System.out.println(timeZoneSH.getId()+dateTimeFormatter2.withZone(timeZoneSH).format(dateTime));
        System.out.println(timeZoneNY.getId()+dateTimeFormatter2.withZone(timeZoneNY).format(dateTime));
        System.out.println(timeZoneJST.getId()+dateTimeFormatter2.withZone(timeZoneJST).format(dateTime));
    }
}

